package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.mapper.AreaMapper;
import com.example.mybatisplus.model.domain.Area;
import com.example.mybatisplus.model.domain.Building;
import com.example.mybatisplus.mapper.BuildingMapper;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.service.BuildingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2024-07-17
 */
@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements BuildingService {

    @Override
    public Page<Building> pageList(Building building, PageDTO dto) {
        QueryWrapper<Building> wrapper =new QueryWrapper<>();

        if(Objects.nonNull(building.getBuildingId())){
            wrapper.eq("buildingId",building.getBuildingId());
        }
        Page<Building> page = new Page<>();
        page.setCurrent(dto.getPageNo()).setSize(dto.getPageSize());
        baseMapper.selectPage(page,wrapper);
        return page;
    }

    @Override
    public boolean removeBuilding(Building building) {
        QueryWrapper<Building> wrapper = new QueryWrapper<>();
        wrapper.eq("buildingId",building.getBuildingId());
        Building one = baseMapper.selectOne(wrapper);
        if(one.getRoomNum() == 0 && one.getBedNum() == 0){ // 该楼栋可被直接删除
            baseMapper.deleteById(one.getBuildingId());
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean forceRemoveBuilding(Building building) {
        QueryWrapper<Building> wrapper = new QueryWrapper<>();
        wrapper.eq("buildingId", building.getBuildingId());
        Building one = baseMapper.selectOne(wrapper);
        one.setRoomNum(0);
        one.setBedNum(0);
        baseMapper.updateById(one);
        baseMapper.deleteById(one.getBuildingId());
        return true;
    }
    @Override
    public List<String> getAllBuildingNames() {
        return baseMapper.getAllBuildingNames();
    }
}
